package com.llc.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.llc.common.exception.BizCodeEnume;
import com.llc.common.to.SkuStockTO;
import com.llc.gulimall.ware.exception.NoStockException;
import com.llc.gulimall.ware.vo.LockStockResult;
import com.llc.gulimall.ware.vo.SkuHasStockVo;
import com.llc.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.llc.gulimall.ware.entity.WareSkuEntity;
import com.llc.gulimall.ware.service.WareSkuService;
import com.llc.common.utils.PageUtils;
import com.llc.common.utils.R;



/**
 * 商品库存
 *
 * @author lls
 * @email leilingchun@163.com
 * @date 2022-01-24 19:32:46
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 锁定订单库存
     */
    @PostMapping("/lock/order")
    public  R orderLockStock(@RequestBody WareSkuLockVo vo){
        try {
            System.out.println("锁库存");
            Boolean stock = wareSkuService.orderLockStock(vo);
            System.out.println("正常");
            return R.ok();
        } catch (NoStockException e){
            return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getCode(), BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 对外提供远程调用结果 用于查询sku是否有库存
     * @return
     * 有错再调整
     */
    //@PostMapping( "/hasStock")
    //public R<List<SkuHasStockVo>> getSkuHasStock(@RequestBody List<Long> skuIds) {
    //    //skuId stock
    //    List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);
    //    R<List<SkuHasStockVo>> ok = new R(vos);
    //    //ok.setData(vos);
    //    System.out.println(ok.getData());
    //    System.out.println("json:"+JSON.toJSONString(ok));
    //    System.out.println(ok);
    //    return (R<List<SkuHasStockVo>>) vos;
    //}
    /**
     * 查询sku是否有库存
     *
     * @return
     */
    @PostMapping(value = "/hasStock")
    public R getSkuHasStock(@RequestBody List<Long> skuIds) {
        //skuId stock
        List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);

        return R.ok().setData(vos);
    }


    @RequestMapping("/stock/batch")
    public R getSkuStockBatch(@RequestBody List<Long> skuIds) {
        List<SkuStockTO> skuStockTOS = wareSkuService.getSkusStock(skuIds);
        R ok = R.ok();
        ok.setData(skuStockTOS);
        return ok;
    }

    @RequestMapping("/stock/{skuId}")
    public R getSkuStock(@PathVariable("skuId") Long skuId) {
        Long stock  = wareSkuService.getSkuStock(skuId);
        return R.ok().setData(stock);
    }




}
